package com.leo;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonBar;
import javafx.scene.control.ButtonType;
import javafx.scene.control.Dialog;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import javafx.util.Callback;

import java.util.Optional;

/**
 * @ClassName DialogDemo
 * @Description 弹窗
 * @Author wangss
 * @date 2020.06.13 17:41
 * @Version 1.0
 */
public class DialogDemo extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        AnchorPane anchorPane = new AnchorPane();
        anchorPane.setStyle("-fx-background-color: #fff2f2");

        Button button = new Button("弹框");

        AnchorPane.setTopAnchor(button, 20d);
        AnchorPane.setLeftAnchor(button, 20d);
        anchorPane.getChildren().addAll(button);
        Scene scene = new Scene(anchorPane);
        primaryStage.setScene(scene);
        primaryStage.setWidth(800d);
        primaryStage.setHeight(800d);
        primaryStage.setTitle("Dialog");
        primaryStage.setResizable(false);
        primaryStage.show();

        button.setOnAction(event -> {
            Dialog<ButtonType> dialog = new Dialog<>();

            dialog.getDialogPane().getButtonTypes().add(ButtonType.CLOSE);
            dialog.getDialogPane().getButtonTypes().add(ButtonType.OK);

            // 可以设置图形
//            dialog.setGraphic();
            dialog.setTitle("新年快乐");
            dialog.setHeaderText("我是标题");
//            dialog.setContentText("我是内容");

            dialog.show();

            // 关闭时监听
            dialog.setOnCloseRequest(event1 -> {

            });

            dialog.setResultConverter(new Callback<ButtonType, ButtonType>() {
                @Override
                public ButtonType call(ButtonType param) {
                    System.out.println(param.getText());
                    return null;
                }
            });

            /*Optional<ButtonType> buttonType = dialog.showAndWait();
            buttonType.ifPresent(buttonType1 -> {
                if (buttonType1.getButtonData() == ButtonBar.ButtonData.OK_DONE) {
                    dialog.setContentText("111111111111111");
                } else {
                    dialog.setContentText("22222222222222222");
                }
            });*/
        });
    }
}
